{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Longest Substring Of All Vowels in Order"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string #sliding-window"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串 #滑动窗口"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: longestBeautifulSubstring"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #所有元音按顺序排布的最长子字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>当一个字符串满足如下条件时，我们称它是 <b>美丽的</b> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>所有 5 个英文元音字母（<code>'a'</code> ，<code>'e'</code> ，<code>'i'</code> ，<code>'o'</code> ，<code>'u'</code>）都必须 <strong>至少</strong> 出现一次。</li>\n",
    "\t<li>这些元音字母的顺序都必须按照 <strong>字典序</strong> 升序排布（也就是说所有的 <code>'a'</code> 都在 <code>'e'</code> 前面，所有的 <code>'e'</code> 都在 <code>'i'</code> 前面，以此类推）</li>\n",
    "</ul>\n",
    "\n",
    "<p>比方说，字符串 <code>\"aeiou\"</code> 和 <code>\"aaaaaaeiiiioou\"</code> 都是 <strong>美丽的</strong> ，但是 <code>\"uaeio\"</code> ，<code>\"aeoiu\"</code> 和 <code>\"aaaeeeooo\"</code> <strong>不是美丽的</strong> 。</p>\n",
    "\n",
    "<p>给你一个只包含英文元音字母的字符串 <code>word</code> ，请你返回 <code>word</code> 中 <strong>最长美丽子字符串的长度</strong> 。如果不存在这样的子字符串，请返回 <code>0</code> 。</p>\n",
    "\n",
    "<p><strong>子字符串</strong> 是字符串中一个连续的字符序列。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>word = \"aeiaaio<strong>aaaaeiiiiouuu</strong>ooaauuaeiu\"\n",
    "<b>输出：</b>13\n",
    "<b>解释：</b>最长子字符串是 \"aaaaeiiiiouuu\" ，长度为 13 。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>word = \"aeeeiiiioooauuu<strong>aeiou</strong>\"\n",
    "<b>输出：</b>5\n",
    "<b>解释：</b>最长子字符串是 \"aeiou\" ，长度为 5 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>word = \"a\"\n",
    "<b>输出：</b>0\n",
    "<b>解释：</b>没有美丽子字符串，所以返回 0 。\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= word.length <= 5 * 10<sup>5</sup></code></li>\n",
    "\t<li><code>word</code> 只包含字符 <code>'a'</code>，<code>'e'</code>，<code>'i'</code>，<code>'o'</code> 和 <code>'u'</code> 。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [longest-substring-of-all-vowels-in-order](https://leetcode.cn/problems/longest-substring-of-all-vowels-in-order/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [longest-substring-of-all-vowels-in-order](https://leetcode.cn/problems/longest-substring-of-all-vowels-in-order/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"aeiaaioaaaaeiiiiouuuooaauuaeiu\"', '\"aeeeiiiioooauuuaeiou\"', '\"a\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        n = len(word)\n",
    "\n",
    "        if n<5:\n",
    "            return 0\n",
    "        i=0\n",
    "        ans =0\n",
    "\n",
    "        while i<n:\n",
    "\n",
    "            while i<n-5 and word[i]!=\"a\":\n",
    "                i+=1\n",
    "            if i >n-5:\n",
    "                break\n",
    "            start = i\n",
    "            cnt =1\n",
    "            i+=1\n",
    "            while i<n and word[i]>=word[i-1]:\n",
    "                if word[i]>word[i-1]:\n",
    "                    cnt+=1\n",
    "                i+=1\n",
    "            \n",
    "            if cnt ==5:\n",
    "                ans = max(ans,i-start)\n",
    "        return ans \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        ans = i = 0\n",
    "        n = len(word)\n",
    "        while i < n:\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i < n and ord(word[i]) - ord(word[i-1]) >= 0:\n",
    "                i += 1\n",
    "            if 'a' in word[start:i] and 'e' in word[start:i] and 'i' in word[start:i] and 'o' in word[start:i] and 'u' in word[start:i]:\n",
    "                ans = max(ans, i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        i,n,ret=0,len(word),0\n",
    "        def get(a,b):\n",
    "            if a==b:\n",
    "                return True\n",
    "            if a=='a' and b=='e':\n",
    "                return True \n",
    "            if a=='e' and b=='i':\n",
    "                return True \n",
    "            if a=='i' and b=='o':\n",
    "                return True \n",
    "            if a=='o' and b=='u':\n",
    "                return True \n",
    "            return False\n",
    "        while i<n:\n",
    "            left=i\n",
    "            while i<n-1 and get(word[i],word[i+1]):\n",
    "                i+=1\n",
    "            if word[left]=='a' and word[i]=='u':\n",
    "                ret=max(ret,i-left+1)\n",
    "            i+=1\n",
    "        return ret\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        ans = 0\n",
    "        start = -1\n",
    "        vowels = {'a':1, 'e':2, 'i':3, 'o':4, 'u':5}\n",
    "        while i < n:\n",
    "            if start == i:\n",
    "                i += 1      # 没走下面的内循环\n",
    "            if i < n and s[i] != 'a':\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            cur = 1\n",
    "            while i < n and (vowels[s[i]] - cur == 0 or vowels[s[i]] - cur == 1):       \n",
    "                cur = vowels[s[i]]\n",
    "                i += 1\n",
    "            if vowels[s[i-1]] == 5:\n",
    "                ans = max(ans, i - start)\n",
    "        \n",
    "        return ans\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        mp = dict()\n",
    "        mp['a'] = ['a', 'e']\n",
    "        mp['e'] = ['e', 'i']\n",
    "        mp['i'] = ['i', 'o']\n",
    "        mp['o'] = ['o', 'u']\n",
    "        mp['u'] = ['u']\n",
    "        i = 0\n",
    "        start = 0\n",
    "        res = 0\n",
    "        length = len(word)\n",
    "        while i <= length-1:\n",
    "            if not (word[i] == 'a'):\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            i += 1\n",
    "            while i <= length-1 and (word[i] in mp[word[i-1]]):\n",
    "                i += 1\n",
    "            # if not i <= length-1:\n",
    "            #     break\n",
    "            # elif word[i-1] != 'u':\n",
    "            #     continue\n",
    "            # elif word[i-1] == 'u':\n",
    "            #     res = max(res, len(word[start : i]))\n",
    "            if (i == length and word[i-1] == 'u') or (word[i-1] == 'u'):\n",
    "                res = max(res, len(word[start : i]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, s: str) -> int:\n",
    "        n = len(s)\n",
    "        i = 0\n",
    "        ans = 0\n",
    "        # start = -1\n",
    "        vowels = {'a':1, 'e':2, 'i':3, 'o':4, 'u':5}\n",
    "        while i < n:\n",
    "            if i < n and s[i] != 'a':  # 起点不是'a'，直接跳过\n",
    "                i += 1\n",
    "                continue  \n",
    "            start = i\n",
    "            i += 1\n",
    "            cur = 1\n",
    "            while i < n and (vowels[s[i]] - cur == 0 or vowels[s[i]] - cur == 1):       \n",
    "                # 按照元音的顺序\n",
    "                cur = vowels[s[i]]  # 更新cur\n",
    "                i += 1\n",
    "            if s[i-1] == 'u':   # 末尾是'u'\n",
    "                ans = max(ans, i - start)\n",
    "        \n",
    "        return ans\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 1.首先如果数组长度小于5的话，不可能满足美丽的定义，将这种情况提前排除\n",
    "# 2.遍历时分了几种情况判断：\n",
    "# - 如果当前字符比上一个不小（顺序意义），那么当前子串长度+1\n",
    "# - 如果当前字符比上一个大，那么子串中元音字母种类+1\n",
    "# - 如果当前字符比上一个小，那么肯定当前字串不美丽，以当前字符为首继续进行遍历\n",
    "# 3.如果当前子字符串没有以a开头的话，那么在进行下一个子字符串开始遍历之前，元音种类一定不会达到5，所以只要判断种类即可\n",
    "# 4.当元音种类为5的时候，持续维护更新最终结果，取出最大值即可\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        # dic = {'a': 1, 'e': 2, 'i': 3, 'o': 4, 'u': 5}\n",
    "\n",
    "        ans = 0\n",
    "        rlen, vcnt = 0, 0\n",
    "        for i, ch in enumerate(word):\n",
    "            if ch >= word[i-1]:\n",
    "                rlen += 1\n",
    "            if ch > word[i-1]:\n",
    "                vcnt += 1\n",
    "            if ch < word[i-1]:\n",
    "                rlen, vcnt = 1, 1\n",
    "            if vcnt == 5:\n",
    "                ans = max(ans, rlen)\n",
    "\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        i, ans, n = 0, 0, len(word)\n",
    "        while i < n:\n",
    "            if word[i] != 'a':\n",
    "                i += 1\n",
    "                continue\n",
    "            start = i\n",
    "            a = set('a')\n",
    "            i += 1\n",
    "            while i < n and word[i - 1] <= word[i]:\n",
    "                a.add(word[i])\n",
    "                i += 1\n",
    "            if len(a) == 5: ans = max(ans, i - start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        last=''\n",
    "        new_word=''\n",
    "        count=[]\n",
    "        for w in word:\n",
    "            if w!=last:\n",
    "                new_word+=w\n",
    "                count.append(1)\n",
    "            else:\n",
    "                count[-1]+=1\n",
    "            last=w\n",
    "        res=[]\n",
    "        for i in range(len(new_word)-4):\n",
    "            if new_word[i:i+5]==\"aeiou\":\n",
    "                res.append(sum(count[i:i+5]))\n",
    "        if not res:\n",
    "            return 0\n",
    "        return max(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        # print(set(word))\n",
    "        # if set(word) != {'a', 'u', 'o', 'e', 'i'}:\n",
    "        #     return 0\n",
    "\n",
    "\n",
    "\n",
    "        # w = set(word)\n",
    "        # if 'a' not in w or 'e' not in w or 'i' not in w or 'o' not in w or 'u' not in w:\n",
    "        #     return 0\n",
    "        # r = re.findall('a+e+i+o+u+', word)\n",
    "        # # print(r)\n",
    "        # if r:\n",
    "        #     # print(max([len(x) for x in r]))\n",
    "        #     return max([len(x) for x in r])\n",
    "        # else:\n",
    "        #     return 0 \n",
    "        # word = 'aaeuaaeioouuui'\n",
    "        # word = \"aeeeiiiioooauuuaeiou\"\n",
    "        # word = \"eieuuoeeueauueeoiiueaeuioeiiieioaouoeeoiiauiueiiuieueeeoaeaeoooiuauiaaueieoeeeeioauaouueiioeiaaaaooeaaaeoouoaoioauoaeuiioaiuuiaaaiieiuiuoiaeuuiooeiiuoaauoioeaeieaeeeiouieuaaaaaeoaeiaeeeaoaeooueieaaaeieooouueaueeiaiuoioiioieuoeeoeaaeeaauoaiaoaauaauioaueiaiooeoiiaiauaooiiauaiaoaoeaioeiaeeuueioauoueuaaiiouoeoeoiaeeooaauieoeaiueioiaieouiaeauaeiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooouuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuiuaeouiioieouieeiuaeeuoiooioiiaoueioeuaeeoaauoaaeeaaoiuueoeaeoaaeueoueeiiiuiouieaiuoieeoooueiiiaeeuaaieeaaoiuuoeuoauouiieuaeeiueaeeeaaoieaeuiuoeuoaaiueieiouiiiaoueuuiuooeioaeooaeueouiaeuaaiueuoeeiouioiiueiueaaiuoaieuiaoeuoeaeaiuaaoeeuouiiioeeoueuuauoieiaoaeeaeioouoaiieueoauaoeeoeouaiaoaueioiuaooouiueuieuauieeoeaiouoaoeeeueuoeuieooaaaaaiuoououiuuueoioieueuueaueaeuaieueeaieioieuuuueoooueueoiaoiuuiooiauaaiiiiuoaaueeaiiaeaauaooooeaaeuiaaouooauaaaaiieiaeeeeeiauiiuaueieuieaoeeiauiuaueouaeoaaoiuaoiaiouaiaeoiaooieeioiieieioeeioeuioeiooouiiioaiaeeaaieouoaooaiaiueeuieoiuaeuieeiuaoaueiouoieauieiuoooieuiaooeaeaoiueaeeuieaioiuoaeieuaaeouuoiieioeoiaeuiiiuiuaaaoeaaaoiuauaiiuaaaaiieeueeoieuiueuaueiiaioaoaoueiieueoauuueeeeuuueoiuuaiaaeuauuiaeuuaeiaiiioiooeaeiaeoeaeouuaouueeiieaoaeoaueuaoouoioeiaiuiouaueuaiaoeouoeoooioiuioaeeoeaaoioeiioiuoaiiuioiiioeuiiaaaieaoaaeoaeieuiiiuieiieaaioaueoiiiuueeiouuueoiiaeuuaeaaiauaeeieoaioiiuueuaoieieiuiaioeaeuiiaioaieeeaoaieoooiuiaeuuuieeaeeeeuoeueioeiiaaieaiaiiieiouueaouoooueeoiauooeieeoeuoueioauieoeuooiiaiueeouiiuuiaiuuouoaaiuiiiouuuaiiiuiiuouiioaoauiaoeiioaooeuoaauaiiieiuueuueioeaioaaieuuoioeaeeeuauouuiaauuiauuuaiiiooaiaeaeeuioaueeaoiaioiieooueaieoeieoiaouuuuioaieauioeuoauauaaeaoueeiaaoooauoioueaieeaaiuuiieieaaaeoiueeoeoeoaaoioaueeoeoiooeiioiaoeieieaeiaaaoeeuoiueaueoeoaueeiioauuuoaiaeaooaiaaauuieaioeaaoeioouaiauiaaoieiiieeioeeuaiauuaeeooiuuuaeeieaeieoaaiioaoooeaaeaooouaaoeaoiiuaiauooaiuoeauoeiaeaaeueeiaiooaeaoioeuuauaioaiiioioeuuueiuaeoeaiiuaioiueooioeiueaaeueeiauoiaoeooiuaeuoaououiiiauuaaeeuaiuueaaoioueouaooaaiieeeiueieeouoieiuoueeueauauoueaooaoiuouuiauaeoiouaoeaauaaaoaaueoaeaoeuaiiiaueeiiaaoeuaouiaaiauoeeueueiaooeiooaeuooiieioaaeiueuieuoueiaeiiiaaueaaaoauiauaooaeoeouaaoeeoieuioeuooeaeeoioeeoouieiouuooeaioooouoaoeeouueioaeuoeoiioeouoeuiuuaaouaiuaioaiiuiiuuieaoiiieaaeeioeeiuuouioiuoaiuuuouoeauuoauoeiuuoiiuauoaeuieaiaaoaoiieoeeiaoaueuuoiaauioueeiueeaouioiiauoeuaooiueaaeuuiiiooioeiuioaeuuaouieeiuooaeeaeiiiaiieoaeeoaoioieiuiaeiaiiiouoeoeaeuiuaoeuiiouoaeioiouioiaouiaoooioooieiieaeeaiaaiuoaaaaeoauioiouaaaoeiiueaooeieieueaoiuiaiueaaauiuiuooeaaaaeoiooeueiaaueeaeaoouoioouoeouaoaooauieeuuoeooaauuoaueoaaauiiueoaauiiaaiaoaiuoeeooiiauaueeuoiuooaiueeeauaoeeaiieeoeaaaoueaeiouaouioauoouaeoiauaauouuuaoeuaaeueeeouoaouueaooiueeauuiiioeaeeieoaoeaooauaiiiueoaiauoueeiooueeiooiaouauiaoooioaaaaoeiaieoeaeaeeoaoaeeaeeuuuuioouaioeouuiuouuoeoaeeaaaeeaoooiaeieiuiuauouuieuiauooioiuoeeoieooeeouoiaaoueoaaiaiuauaauouiauiuoiioiiiauiuooiaooeeiuoeaoaeouuauueiueiuoioeiiaiioueeuiiiueoaieuueaoaiioeeuuauuuiuaueooeaaeeioieuieuuieaeoeoaaoeaoaoueiiaueuouiuuaauiiiuuoaaouaeioioooieoaioeouiiuoeaiaiiueoauaaiaeaaeouaeoiouieuaiaeuieuuioueiueueuaooiiouaioeuooeuaaoooieuauuaueiieoeiouaoaeuiaoaooeoeiaoieeaiiuaeaeauoaieeoeoeoauiauiaooouioaoiueuiueaueaaaoaouoeauueaieeuaiueiuauiueaoieoaioouiaaeuiiiueuaiuaiaeiaouooeioaoeaoiaieaeiuouaieeieoaiaaoioaeau\"\n",
    "\n",
    "        # 法一：\n",
    "        # a, e, i, o, u = 0, 0, 0, 0, 0\n",
    "        # max_len = 0\n",
    "        # max_str = ''\n",
    "        # for item in word:\n",
    "        #     if not(e + i + o + u) and item == 'a':\n",
    "        #         a +=1\n",
    "        #     elif a and not (i + o + u) and item == 'e':\n",
    "        #         e += 1\n",
    "        #     elif a and e and not (o + u) and item == 'i':\n",
    "        #         i += 1\n",
    "        #     elif a and e and i and not (u) and item == 'o':\n",
    "        #         o += 1\n",
    "        #     elif a and e and i and o and item == 'u':\n",
    "        #         u += 1\n",
    "        #     else:\n",
    "        #         # print(a, e, i, o, u)\n",
    "        #         if a and e and i and o and u and a+e+i+o+u > max_len:\n",
    "        #             # print(a, e, i, o, u)\n",
    "        #             max_len = a+e+i+o+u\n",
    "        #             max_str = (a, e, i, o, u)\n",
    "        #         a, e, i, o, u = 0, 0, 0, 0, 0\n",
    "\n",
    "        #         if item == 'a':\n",
    "        #             a += 1\n",
    "        # if a and e and i and o and u and a+e+i+o+u > max_len:\n",
    "        #     # print(a, e, i, o, u)\n",
    "        #     max_len = a+e+i+o+u\n",
    "        #     max_str = (a, e, i, o, u)\n",
    "        # # print(max_len)\n",
    "        # # print(max_str)\n",
    "        # return max_len\n",
    "\n",
    "\n",
    "        # 法二：\n",
    "        # singlar_word = ''\n",
    "        # per_word_cnt = []\n",
    "        # for i in range(len(word)):\n",
    "        #     if i == 0 or word[i] != word[i-1]:\n",
    "        #         singlar_word += word[i]\n",
    "        #         per_word_cnt.append(1)\n",
    "        #     else:\n",
    "        #         per_word_cnt[-1] += 1\n",
    "\n",
    "        # print(singlar_word)\n",
    "        # print(per_word_cnt)\n",
    "\n",
    "        # res=0\n",
    "        # for i in range(len(word)-4):\n",
    "        #     if singlar_word[i:i+5] == 'aeiou':\n",
    "        #         res = max(res, sum(per_word_cnt[i:i+5]))\n",
    "        # print(res)\n",
    "        # return res\n",
    "\n",
    "\n",
    "        # word = \"aeiaaioaaaaeiiiiouuuooaauuaeiu\" # 13  解释：最长子字符串是 \"aaaaeiiiiouuu\" ，长度为 13 。\n",
    "        \n",
    "        word += ' '\n",
    "\n",
    "        shrink_word = ''\n",
    "        char_cnt = []\n",
    "\n",
    "        cur_cnt = 0\n",
    "        flag = False\n",
    "        pre_char = word[0]\n",
    "        for i in word:\n",
    "            if i == pre_char:\n",
    "                flag = True\n",
    "                cur_cnt += 1\n",
    "            elif i != pre_char:\n",
    "                shrink_word += pre_char\n",
    "                char_cnt.append(cur_cnt)\n",
    "                cur_cnt = 1\n",
    "                \n",
    "            pre_char = i\n",
    "\n",
    "        print(shrink_word)\n",
    "        print(char_cnt)\n",
    "\n",
    "        max_len = 0\n",
    "        for i in range(len(shrink_word)):\n",
    "            if shrink_word[i:i+5] == 'aeiou' and sum(char_cnt[i:i+5]) > max_len:\n",
    "                max_len = sum(char_cnt[i:i+5])\n",
    "\n",
    "        print(max_len)\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        dic = {'a': 1, 'e': 2, 'i': 3, 'o': 4, 'u': 5}\n",
    "        n = len(word)\n",
    "        i = 0\n",
    "\n",
    "        ans = 0\n",
    "        while i < n:\n",
    "            st = i\n",
    "            if word[st] != 'a':\n",
    "                i += 1\n",
    "                continue\n",
    "                \n",
    "            i += 1\n",
    "            while i < n and 0 <= dic[word[i]] - dic[word[i-1]] <= 1:\n",
    "                i += 1\n",
    "            \n",
    "            if word[i-1] == 'u':\n",
    "                ans = max(ans, i - st)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\"\"\"\n",
    "比较他出现的前后顺序，可以使用index，用index来比较他的位置\n",
    "valid substring, 有5个元音，并且按照顺序，求出最长的长度\n",
    "\n",
    "使用prev_char 和当前char进行比较，然后判断是否substring是按照\n",
    "aeiou的顺序进行的\n",
    "\n",
    "遍历word, 如果是元音并且在正确的顺序上，cur_len就累加\n",
    "    当这个substring有5个元音的时候，才可以更新Res\n",
    "如果碰到不符合条件的，就重置cur_len, 并且重置visited\n",
    "每次都记录最长的cur_len\n",
    "\n",
    "如果最长的子字符串没有包含所有元音字母，则返回 0\n",
    "\n",
    "\n",
    "在遇到 \"uuaeiou\" 这样的例子时，如果在 \"uu\" 失败后，prev_char 是 'u'\n",
    "怎么初始成a,然后比较，因为后面他碰到a,就自己开始新的一轮了，prev_char会被设成a,有新的set\n",
    "每次不符合条件的，都会进入新的一轮重置\n",
    "\"\"\"\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        res = 0\n",
    "        cur_len = 0\n",
    "        prev_char = 'a'\n",
    "        visited = set()\n",
    "        vowels = 'aeiou'\n",
    "\n",
    "        for char in word:\n",
    "            # print(prev_char)\n",
    "            if char in vowels and vowels.index(char) >= vowels.index(prev_char):\n",
    "                print(char)\n",
    "                cur_len += 1\n",
    "                visited.add(char)\n",
    "                if len(visited) == 5:\n",
    "                    res = max(res, cur_len)\n",
    "            else:\n",
    "                cur_len = 1 if char == 'a' else 0\n",
    "                visited = {char} if char == 'a' else set()\n",
    "            prev_char = char\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        all=False\n",
    "        str=''\n",
    "        hash={'a':1,'e':2,'i':3,'o':4,'u':5}\n",
    "        j=0\n",
    "        answer=[]\n",
    "        x = 0\n",
    "        for i in word:\n",
    "            if hash[i]>=x:\n",
    "                x=hash[i]\n",
    "                str=str+i\n",
    "\n",
    "            else:\n",
    "                answer.append(str)\n",
    "                x = hash[i]\n",
    "                str=i\n",
    "                j+=1\n",
    "        answer.append(str)\n",
    "        le=0\n",
    "\n",
    "        for i in range(len(answer)):\n",
    "            ao = 0\n",
    "            eo = 0\n",
    "            io = 0\n",
    "            oo = 0\n",
    "            uo = 0\n",
    "            for j in answer[i]:  # 判断是否有全\n",
    "                if j == 'a':\n",
    "                    ao = 1\n",
    "                if j == 'e':\n",
    "                    eo = 1\n",
    "                if j == 'o':\n",
    "                    oo = 1\n",
    "                if j == 'i':\n",
    "                    io = 1\n",
    "                if j == 'u':\n",
    "                    uo = 1\n",
    "            if ao + eo + oo + io + uo == 5:\n",
    "                all = True\n",
    "            else:\n",
    "                all=False\n",
    "            if all:\n",
    "                if len(answer[i]) > le:\n",
    "                    print(answer[i])\n",
    "                    le = len(answer[i])\n",
    "        return(le)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        i,n=0,len(word)\n",
    "        \n",
    "        ans=0\n",
    "        while i<n :\n",
    "            start=i\n",
    "            s=word[i]\n",
    "            while i<n and word[i]>=s[-1]:\n",
    "                s+=word[i]\n",
    "                i+=1\n",
    "                \n",
    "            if len(set(word[start:i]))==5 :\n",
    "                ans=max(ans,i-start)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # # 计数\n",
    "\n",
    "\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        lst = []\n",
    "        pre = word[0]\n",
    "        cnt = 1\n",
    "        for w in word[1:]:\n",
    "            if w == pre:\n",
    "                cnt += 1\n",
    "            else:\n",
    "                lst.append([pre, cnt])\n",
    "                pre = w\n",
    "                cnt = 1\n",
    "\n",
    "        lst.append([pre, cnt])\n",
    "\n",
    "        ans = 0\n",
    "        m = len(lst)\n",
    "        i = 0\n",
    "        while i < m - 4:\n",
    "            if lst[i][0] != \"a\":\n",
    "                i += 1\n",
    "            elif [lst[i + j][0] for j in range(5)] == [\"a\", \"e\", \"i\", \"o\", \"u\"]:\n",
    "                cur = sum([lst[i + j][1] for j in range(5)])\n",
    "                ans = ans if ans > cur else cur\n",
    "                i += 5\n",
    "            else:\n",
    "                i += 1\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        wc = []\n",
    "        last = word[0]\n",
    "        count = 1\n",
    "        for c in word[1:]:\n",
    "            if c == last:\n",
    "                count += 1\n",
    "            else:\n",
    "                wc.append((last,count))\n",
    "                count = 1\n",
    "                last = c\n",
    "            \n",
    "        wc.append((last,count))\n",
    "\n",
    "        last = ''\n",
    "        count = 0\n",
    "\n",
    "        next_ = {'a':'e', 'e':'i', 'i':'o', 'o':'u'}\n",
    "        res = 0\n",
    "\n",
    "        for i in range(len(wc)):\n",
    "            # print(i,count)\n",
    "            if last == '' and wc[i][0] != 'a':continue\n",
    "            elif last == '' and wc[i][0] == 'a':\n",
    "                last = 'a'\n",
    "                count = wc[i][1]\n",
    "                continue\n",
    "                \n",
    "            if wc[i][0] == next_[last]:\n",
    "                last = wc[i][0]\n",
    "                count += wc[i][1]\n",
    "                if wc[i][0] == 'u':\n",
    "                    res = max(res, count)\n",
    "                    last = ''\n",
    "                    count = 0\n",
    "            else:\n",
    "                if wc[i][0] != 'a':\n",
    "                    last = ''\n",
    "                    count = 0\n",
    "                else:\n",
    "                    last = 'a'\n",
    "                    count = wc[i][1]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        cnt = collections.defaultdict(int)\n",
    "        cs=0\n",
    "        st = collections.deque()\n",
    "        ans = 0\n",
    "        for i,c in enumerate(word):\n",
    "            cnt[c]+=1\n",
    "            if cnt[c]==1:\n",
    "                cs+=1\n",
    "            if st and c<st[-1]:\n",
    "                cs=1\n",
    "                cnt.clear()\n",
    "                st.clear()\n",
    "                st.append(c)\n",
    "                cnt[c]=1\n",
    "                cs = 1 \n",
    "                continue\n",
    "            st.append(c)\n",
    "            if (cs==5):\n",
    "                ans = max(ans,len(st))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        q = collections.deque()\n",
    "        ans = 0\n",
    "        for i in range(len(word)):\n",
    "            if not q:\n",
    "                q.append(word[i])\n",
    "            else:\n",
    "                if word[i] >= q[-1]:\n",
    "                    q.append(word[i])\n",
    "                else:\n",
    "                    # print(q)\n",
    "                    if len(set(q)) == 5:\n",
    "                        ans = max(ans, len(q))\n",
    "                    q = collections.deque()\n",
    "                    q.append(word[i])\n",
    "        if len(set(q)) == 5:\n",
    "            ans = max(ans, len(q))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        queue = deque()\n",
    "        setq = set()\n",
    "        queue.append(word[0])\n",
    "        setq.add(word[0])\n",
    "        result = 0\n",
    "        for i in range(1,len(word)):\n",
    "            if queue and queue[-1] > word[i]:\n",
    "                if len(setq) == 5:\n",
    "                    result = max(result,len(queue))\n",
    "                queue.clear()\n",
    "                setq.clear()\n",
    "            queue.append(word[i])\n",
    "            setq.add(word[i])\n",
    "        if len(setq) == 5:\n",
    "            result = max(result,len(queue))\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        Pstr = ''\n",
    "        Pstr1 = ''\n",
    "        tag = 0\n",
    "        while tag < len(word):\n",
    "            # tag += 1\n",
    "            if len(Pstr) != 0:\n",
    "                i = Pstr[-1]\n",
    "                if i == 'a':\n",
    "                    if word[tag] == 'a' or word[tag] =='e':\n",
    "                        Pstr = Pstr + word[tag]\n",
    "                    else:\n",
    "                        Pstr = ''\n",
    "                elif i != 'u' and word[tag] == 'a':\n",
    "                    Pstr = 'a'\n",
    "                elif i == 'e':\n",
    "                    if word[tag] == 'e' or word[tag] =='i':\n",
    "                        Pstr = Pstr + word[tag]\n",
    "                    else:\n",
    "                        Pstr = ''\n",
    "                elif i == 'i':\n",
    "                    if word[tag] == 'o' or word[tag] =='i':\n",
    "                        Pstr = Pstr + word[tag]\n",
    "                    else:\n",
    "                        Pstr = ''\n",
    "                elif i == 'o':\n",
    "                    if word[tag] == 'o' or word[tag] =='u':\n",
    "                        Pstr = Pstr + word[tag]\n",
    "                    else:\n",
    "                        Pstr = ''\n",
    "                elif i == 'u':\n",
    "                    if word[tag] == 'u':\n",
    "                        Pstr = Pstr + word[tag]\n",
    "                    else:\n",
    "                        if Pstr1 == '':\n",
    "                            Pstr1 = Pstr\n",
    "                            if word[tag] == 'a':\n",
    "                                Pstr = 'a'\n",
    "                            else:\n",
    "                                Pstr = ''\n",
    "                        elif Pstr1 != '' and len(Pstr) > len(Pstr1):\n",
    "                            Pstr1 = Pstr\n",
    "                            if word[tag] == 'a':\n",
    "                                Pstr = 'a'\n",
    "                            else:\n",
    "                                Pstr = ''\n",
    "                        elif word[tag] == 'a':\n",
    "                            Pstr = 'a'\n",
    "                        else:\n",
    "                            Pstr = ''\n",
    "            else:\n",
    "                if word[tag] == 'a':\n",
    "                    Pstr = 'a'\n",
    "            tag += 1\n",
    "        Fstr = set(Pstr)\n",
    "        if Pstr1 != '':\n",
    "            return len(Pstr1)\n",
    "        elif Pstr1 == '' and len(Fstr) == 5:\n",
    "            return len(Pstr)\n",
    "        else:\n",
    "            return 0\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        dic={\"a\":1,\"e\":2,\"i\":3,\"o\":4,\"u\":5}\n",
    "        flag=False\n",
    "        NICE={}\n",
    "        t=\"\"\n",
    "        count=0\n",
    "        #aeiaaioaaaaeiiiiouuuooaauuaeiu\n",
    "        dic_t={\"a\":0,\"e\":0,\"i\":0,\"o\":0,\"u\":0}\n",
    "        for i in word:\n",
    "            count+=1\n",
    "            if i in dic:\n",
    "                if len(t)==0:\n",
    "                    if i=='a':\n",
    "                        t+='a'\n",
    "                        dic_t[i]=1\n",
    "                    else:\n",
    "                        continue\n",
    "                else:\n",
    "                    if dic[t[-1]]<=dic[i]:\n",
    "                        t+=i\n",
    "                        dic_t[i]=1\n",
    "                    elif sum(dic_t.values())==5:\n",
    "                        NICE[t] = len(t)\n",
    "                        t=\"\"\n",
    "                        for k in dic_t:\n",
    "                            dic_t[k]=0\n",
    "                        if i=='a':\n",
    "                            t += 'a'\n",
    "                            dic_t[i] = 1\n",
    "                    else:\n",
    "                        t=\"\"\n",
    "                        for k in dic_t:\n",
    "                            dic_t[k]=0\n",
    "                        if i=='a':\n",
    "                            t += 'a'\n",
    "                            dic_t[i] = 1\n",
    "\n",
    "\n",
    "            else:\n",
    "                if sum(dic_t.values())==5:\n",
    "                        NICE[t] = len(t)\n",
    "                t=\"\"\n",
    "                for k in dic_t:\n",
    "                    dic_t[k]=0\n",
    "                continue\n",
    "            if count==len(word):\n",
    "                if sum(dic_t.values())==5:\n",
    "                    NICE[t]=len(t)\n",
    "        \n",
    "        if len(NICE)==0:\n",
    "            print(0)\n",
    "            return 0\n",
    "        else:\n",
    "            print(max(NICE.values()))\n",
    "            return max(NICE.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        i, n = 0, len(word)\n",
    "        ans = 0\n",
    "        while i < n:\n",
    "            if word[i] == 'a':\n",
    "                lst = 'a'\n",
    "                i += 1\n",
    "                s = \"a\"\n",
    "                while i < n:\n",
    "                    if word[i] >= lst:\n",
    "                        s += word[i]\n",
    "                        lst = word[i]\n",
    "                        i += 1\n",
    "                    else:\n",
    "                        break\n",
    "                if len(set(s)) == 5:\n",
    "                    ans = max(ans, len(s))\n",
    "            else:\n",
    "                i += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# target = 'aeiou'\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        target = {'a': 1, 'e': 2, \"i\": 3, \"o\": 4, \"u\": 5}\n",
    "\n",
    "        max_len = 0\n",
    "        sub = []\n",
    "        tmp = \"\"\n",
    "        n = len(word)\n",
    "        count = 0\n",
    "        if len(word) < 5:\n",
    "            return 0\n",
    "        for i in word:\n",
    "            count += 1\n",
    "            if i in target:\n",
    "                if not tmp and target[i] == 1:\n",
    "                    tmp += i\n",
    "                    continue\n",
    "                if tmp:\n",
    "                    if (target[i] - target[tmp[-1]]) in [0, 1]:\n",
    "                        tmp += i\n",
    "                        if count == n:\n",
    "                            # 循环结束\n",
    "                            sub.append(tmp)\n",
    "                            if max_len < len(tmp) and tmp[-1] == 'u':\n",
    "                                max_len = len(tmp)\n",
    "                    else:\n",
    "                        sub.append(tmp)\n",
    "                        if max_len < len(tmp) and tmp[-1] == 'u':\n",
    "                            max_len = len(tmp)\n",
    "                        if i == 'a':\n",
    "                            tmp = \"a\"\n",
    "                        else:\n",
    "\n",
    "                            tmp = ''\n",
    "            else:\n",
    "                sub.append(tmp)\n",
    "                if max_len < len(tmp) and tmp[-1] == 'u':\n",
    "                    max_len = len(tmp)\n",
    "                tmp = ''\n",
    "            # 处理头部\n",
    "            # if i == 'a':\n",
    "            #     if tmp:\n",
    "            #         sub.append(tmp)\n",
    "            #         tmp = \"a\"\n",
    "            #         ind = 0\n",
    "            #     else:\n",
    "            #         tmp += 'a'\n",
    "            #     continue\n",
    "            #\n",
    "            # if ind < 4:\n",
    "            #     if i == target[ind]:\n",
    "            #         tmp += i\n",
    "            #         continue\n",
    "            #     elif i == target[ind + 1]:\n",
    "            #         tmp += i\n",
    "            #         ind += 1\n",
    "            #     else:\n",
    "            #         sub.append(tmp)\n",
    "            #         ind = 0\n",
    "            #         tmp = ''\n",
    "            # elif ind == 4:\n",
    "            #     if i == target[ind]:\n",
    "            #         tmp += i\n",
    "            #         continue\n",
    "            #     else:\n",
    "            #         sub.append(tmp)\n",
    "            #         ind = 0\n",
    "            #         tmp = ''\n",
    "\n",
    "        # print(sub)\n",
    "        return max_len\n",
    "    \n",
    "\n",
    "\n",
    "# if __name__ == '__main__':\n",
    "#     s = input()\n",
    "#     print(Solution().longestBeautifulSubstring(s))\n",
    "#     # check(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        # vowel = {'a': 0, 'e': 1, 'i': 2, 'o': 3, 'u': 4}\n",
    "        # stack, res = [], 0\n",
    "        # for i in range(len(word)):\n",
    "        #     if not stack:\n",
    "        #         if word[i] == 'a':\n",
    "        #             stack = [0, i]\n",
    "        #     else:\n",
    "        #         if vowel[word[i]] == stack[0] + 1:\n",
    "        #             stack[0] += 1\n",
    "        #         elif vowel[word[i]] == stack[0]:\n",
    "        #             continue\n",
    "        #         else:\n",
    "        #             if stack[0] == 4:\n",
    "        #                 res = max(res, i - stack[1])\n",
    "        #             stack = []\n",
    "        #             if word[i] == 'a':\n",
    "        #                 stack = [0, i]\n",
    "\n",
    "        # if stack and stack[0] == 4:\n",
    "        #     res = max(res, len(word) - stack[1])\n",
    "        # return res\n",
    "\n",
    "        s='aeiou'\n",
    "        dict_s = {'a':0,'e':0,'i':0,'o':0,'u':0}\n",
    "        cur = ''\n",
    "        l = 0\n",
    "        res=0\n",
    "        for i in range(len(word)):\n",
    "            if cur == '':\n",
    "                if word[i]=='a':\n",
    "                    cur += word[i]\n",
    "                    l +=1\n",
    "                    dict_s[cur]=1\n",
    "            else:\n",
    "                if s.index(cur[-1])<=s.index(word[i]):\n",
    "                    cur += word[i]\n",
    "                    l +=1\n",
    "                    dict_s[cur[-1]]=1\n",
    "                    if i == len(word)-1:\n",
    "                        t = True\n",
    "                        for k in s:\n",
    "                            if dict_s[k]==0:\n",
    "                                t=False\n",
    "                                break\n",
    "                        if t:\n",
    "                            res = max(res,len(cur))\n",
    "                else:\n",
    "                    t = True\n",
    "                    for k in s:\n",
    "                        if dict_s[k]==0:\n",
    "                            t=False\n",
    "                            break\n",
    "                    if t:\n",
    "                        res = max(res,len(cur))\n",
    "\n",
    "                    dict_s = {'a':0,'e':0,'i':0,'o':0,'u':0}\n",
    "                    cur = ''\n",
    "                    l = 0\n",
    "                    if word[i]=='a':\n",
    "                        cur += word[i]\n",
    "                        l +=1\n",
    "                        dict_s[cur[-1]]=1\n",
    "        return res\n",
    "\n",
    "\n",
    "                    \n",
    "                    \n",
    "\n",
    "                \n",
    "\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# target = 'aeiou'\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        target = {'a': 1, 'e': 2, \"i\": 3, \"o\": 4, \"u\": 5}\n",
    "\n",
    "        max_len = 0\n",
    "        sub = []\n",
    "        tmp = \"\"\n",
    "        n = len(word)\n",
    "        count = 0\n",
    "        if len(word) < 5:\n",
    "            return 0\n",
    "        for i in word:\n",
    "            count += 1\n",
    "            if i in target:\n",
    "                if not tmp and target[i] == 1:\n",
    "                    tmp += i\n",
    "                    continue\n",
    "                if tmp:\n",
    "                    if (target[i] - target[tmp[-1]]) in [0, 1]:\n",
    "                        tmp += i\n",
    "                        if count == n:\n",
    "                            # 循环结束\n",
    "                            sub.append(tmp)\n",
    "                            if max_len < len(tmp) and tmp[-1] == 'u':\n",
    "                                max_len = len(tmp)\n",
    "                    else:\n",
    "                        sub.append(tmp)\n",
    "                        if max_len < len(tmp) and tmp[-1] == 'u':\n",
    "                            max_len = len(tmp)\n",
    "                        if i == 'a':\n",
    "                            tmp = \"a\"\n",
    "                        else:\n",
    "\n",
    "                            tmp = ''\n",
    "            else:\n",
    "                sub.append(tmp)\n",
    "                if max_len < len(tmp) and tmp[-1] == 'u':\n",
    "                    max_len = len(tmp)\n",
    "                tmp = ''\n",
    "            # 处理头部\n",
    "            # if i == 'a':\n",
    "            #     if tmp:\n",
    "            #         sub.append(tmp)\n",
    "            #         tmp = \"a\"\n",
    "            #         ind = 0\n",
    "            #     else:\n",
    "            #         tmp += 'a'\n",
    "            #     continue\n",
    "            #\n",
    "            # if ind < 4:\n",
    "            #     if i == target[ind]:\n",
    "            #         tmp += i\n",
    "            #         continue\n",
    "            #     elif i == target[ind + 1]:\n",
    "            #         tmp += i\n",
    "            #         ind += 1\n",
    "            #     else:\n",
    "            #         sub.append(tmp)\n",
    "            #         ind = 0\n",
    "            #         tmp = ''\n",
    "            # elif ind == 4:\n",
    "            #     if i == target[ind]:\n",
    "            #         tmp += i\n",
    "            #         continue\n",
    "            #     else:\n",
    "            #         sub.append(tmp)\n",
    "            #         ind = 0\n",
    "            #         tmp = ''\n",
    "\n",
    "        # print(sub)\n",
    "        return max_len\n",
    "    \n",
    "\n",
    "\n",
    "# if __name__ == '__main__':\n",
    "#     s = input()\n",
    "#     print(Solution().longestBeautifulSubstring(s))\n",
    "#     # check(s)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "\n",
    "        dic = {'a':'1','e':'2','i':'3','o':'4','u':'5'}\n",
    "        word2 = ''\n",
    "        for w in word:\n",
    "            word2 += dic[w]\n",
    "        i = 0\n",
    "        ret=0\n",
    "        for j in range(1,len(word2)):\n",
    "            if word2[j] < word2[j-1] :\n",
    "                if len(set(list(word2[i:j]))) == 5:\n",
    "                    ret = max(ret,j-i)\n",
    "                i = j  \n",
    "        if len(word2) > 1 and len(set(word2[i:j+1])) == 5:\n",
    "            ret = max(ret,j+1-i)\n",
    "        return ret\n",
    "                    \n",
    "\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def deal(self,start, word, tmp_set):\n",
    "        tmp = \"a\"\n",
    "        for i in range(start, len(word)):\n",
    "            if ord(tmp[-1]) <= ord(word[i]):\n",
    "                tmp = tmp + word[i]\n",
    "                if i==len(word)-1:\n",
    "                    if ord(tmp[-1]) >= ord(\"u\"):\n",
    "                        if {\"a\", 'e', \"i\", \"o\", \"u\"} <= set(tmp):\n",
    "                            tmp_set.add(tmp)\n",
    "\n",
    "            else:\n",
    "                if ord(tmp[-1]) >= ord(\"u\"):\n",
    "                    if {\"a\", 'e', \"i\", \"o\", \"u\"} <= set(tmp):\n",
    "                        tmp_set.add(tmp)\n",
    "                start = word.find(\"a\",i)\n",
    "                if (start != -1):\n",
    "                    self.deal(start, word, tmp_set)\n",
    "                break\n",
    "        return tmp_set\n",
    "#aeiaaioaaaaeiiiiouuuooaauuaeiu\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        start = word.find(\"a\")\n",
    "        tmp_set = set()\n",
    "        # a = self.deal(start, word, tmp_set)\n",
    "        # return a\n",
    "        tmp_set=list(self.deal(start,word,tmp_set))\n",
    "        if len(tmp_set) == 0:\n",
    "            return 0\n",
    "        tmp_set.sort(key=lambda item:len(item))\n",
    "        return len(tmp_set[-1][1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        group= []\n",
    "        pre = \"x\"\n",
    "        if \"a\" not in word or \"e\" not in word or \"i\" not in word or \"o\" not in word or \"u\" not in word:\n",
    "            return 0\n",
    "        for c in word:\n",
    "            if c==\"a\":\n",
    "                if pre==\"a\":\n",
    "                    group[-1][c]+=1\n",
    "                else:\n",
    "                    group.append({c:1})\n",
    "            if c==\"e\":\n",
    "                if pre==\"a\":\n",
    "                    group[-1][c]=1\n",
    "                elif pre==\"e\":\n",
    "                    group[-1][c]+=1\n",
    "                else:\n",
    "                    group.append({c:1})\n",
    "            if c==\"i\":\n",
    "                if pre==\"e\":\n",
    "                    group[-1][c]=1\n",
    "                elif pre==\"i\":\n",
    "                    group[-1][c]+=1\n",
    "                else:\n",
    "                    group.append({c:1})\n",
    "            if c==\"o\":\n",
    "                if pre==\"i\":\n",
    "                    group[-1][c]=1\n",
    "                elif pre==\"o\":\n",
    "                    group[-1][c]+=1\n",
    "                else:\n",
    "                    group.append({c:1})\n",
    "            if c==\"u\":\n",
    "                if pre==\"o\":\n",
    "                    group[-1][c]=1\n",
    "                elif pre==\"u\":\n",
    "                    group[-1][c]+=1\n",
    "                else:\n",
    "                    group.append({c:1})\n",
    "            pre = c\n",
    "        ans = 0\n",
    "        for has in group:\n",
    "            if \"a\" in has and \"e\" in has and \"i\" in has and \"o\" in has and \"u\" in has:\n",
    "                length = has[\"a\"] + has[\"e\"] + has[\"i\"] + has[\"o\"] + has[\"u\"]\n",
    "                ans = max(ans, length)\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        l,r,length=0,0,len(word)\n",
    "        nextneed=['a','a']\n",
    "        ans=0\n",
    "        while r<length:\n",
    "            if word[l]!='a':\n",
    "                l+=1\n",
    "                r=l\n",
    "                continue\n",
    "            curchar=word[r]\n",
    "            if curchar==nextneed[-1] or curchar==nextneed[-2]:\n",
    "                if curchar==nextneed[-1]:\n",
    "                    if nextneed[-1] == 'a':\n",
    "                        nextneed.append('e')\n",
    "                    elif nextneed[-1] == 'e':\n",
    "                         nextneed.append('i')\n",
    "                    elif nextneed[-1] == 'i':\n",
    "                         nextneed.append('o')\n",
    "                    elif nextneed[-1] == 'o':\n",
    "                         nextneed.append('u')\n",
    "                    else:\n",
    "                        nextneed.append('u')\n",
    "                        ans = max(ans, r - l + 1)\n",
    "                r+=1\n",
    "            else:\n",
    "                l=r\n",
    "                nextneed = ['a', 'a']\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        l,r,length=0,0,len(word)\n",
    "        nextneed=['a','a']\n",
    "        ans=0\n",
    "        while r<length:\n",
    "            if word[l]!='a':\n",
    "                l+=1\n",
    "                r=l\n",
    "                continue\n",
    "            curchar=word[r]\n",
    "            if curchar==nextneed[-1] or curchar==nextneed[-2]:\n",
    "                if curchar==nextneed[-1]:\n",
    "                    if nextneed[-1] == 'a':\n",
    "                        nextneed.append('e')\n",
    "                    elif nextneed[-1] == 'e':\n",
    "                         nextneed.append('i')\n",
    "                    elif nextneed[-1] == 'i':\n",
    "                         nextneed.append('o')\n",
    "                    elif nextneed[-1] == 'o':\n",
    "                         nextneed.append('u')\n",
    "                    else:\n",
    "                        nextneed.append('u')\n",
    "                        ans = max(ans, r - l + 1)\n",
    "                r+=1\n",
    "            else:\n",
    "                l=r\n",
    "                nextneed = ['a', 'a']\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        alphbet = ['a','e','i','o','u']\n",
    "        tmp = []\n",
    "        alph = set()\n",
    "        res = 0\n",
    "        for i in word:\n",
    "            if i not in alphbet: return 0\n",
    "            \n",
    "            if tmp == []:\n",
    "                tmp.append(i)\n",
    "                alph.add(i)\n",
    "                continue\n",
    "            else:\n",
    "                if i >= tmp[-1]:\n",
    "                    tmp.append(i)\n",
    "                    alph.add(i)\n",
    "                else:\n",
    "                    if len(alph) == 5:\n",
    "                        res = max(res, len(tmp))\n",
    "                    tmp = []\n",
    "                    alph = set()\n",
    "                    tmp.append(i)\n",
    "                    alph.add(i)\n",
    "        if len(alph) == 5:\n",
    "            res = max(res, len(tmp))\n",
    "        return res\n",
    "  \n",
    "                        \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        l = len(word)\n",
    "        w = []\n",
    "        for i in range(l):\n",
    "            if word[i] not in ['a', 'e', 'i', 'o', 'u']:\n",
    "                w.append('*')\n",
    "            else:\n",
    "                w.append(word[i])\n",
    "        demand = {}\n",
    "        demand['*'] = ['a', '*']\n",
    "        demand['a'] = ['a', 'e', '*']\n",
    "        demand['e'] = ['e', 'i', '*']\n",
    "        demand['i'] = ['i', 'o', '*']\n",
    "        demand['o'] = ['o', 'u', '*']\n",
    "        demand['u'] = ['u', '*']\n",
    " \n",
    "        ans = 0\n",
    "        p = 0\n",
    "        cur = ''\n",
    "        while p < l and w[p] not in ['a', '*']:\n",
    "            p += 1\n",
    "\n",
    "        while p < l:\n",
    "            cur = w[p]\n",
    "            r = p + 1\n",
    "            while r < l and w[r] in demand[cur]:\n",
    "                if w[r] != '*':\n",
    "                    cur = w[r]\n",
    "                r += 1 \n",
    "            if cur == 'u':\n",
    "                ans = max(ans, r - p)\n",
    "            while r < l and w[r] not in ['a', '*']:\n",
    "                r += 1\n",
    "            p = r\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        temp_list = []\n",
    "        temp_set = set()\n",
    "        left = right = 0\n",
    "        res = 0\n",
    "        while right<len(word):\n",
    "            if len(temp_list)==0 or word[right]>=temp_list[-1]:\n",
    "                temp_list.append(word[right])\n",
    "                temp_set.add(word[right])\n",
    "                if len(temp_set)==5:\n",
    "                    res = max(res,len(temp_list))\n",
    "            else:\n",
    "                temp_list = [word[right]]\n",
    "                temp_set = set(word[right])\n",
    "                left = right\n",
    "            right += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        elm = ['a','e','i','o','u']\n",
    "        stk = []\n",
    "        num = []\n",
    "        for i in range(len(word)):\n",
    "            if word[i] in elm:\n",
    "                if stk and (elm.index(word[i]) != elm.index(stk[-1]) and elm.index(word[i]) != elm.index(stk[-1]) + 1):\n",
    "                    if stk[-1] == 'u' and stk[0] == 'a':\n",
    "                        num.append(len(stk))\n",
    "                    stk = []\n",
    "                    if word[i] == 'a':\n",
    "                        stk.append('a')\n",
    "                    continue\n",
    "            else:\n",
    "                if stk and stk[-1] == 'u':\n",
    "                    num.append(len(stk))\n",
    "                stk = []\n",
    "                continue\n",
    "            stk.append(word[i])\n",
    "            #print(stk)\n",
    "        if stk and stk[-1] == 'u' and stk[0] == 'a':\n",
    "            num.append(len(stk))\n",
    "        return max(num) if num else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        m = {}\n",
    "        arr = []\n",
    "        res = 0\n",
    "        l, r = 0, 0\n",
    "        while r < len(word):\n",
    "            if len(arr) == 0 or arr[-1] <= word[r]:\n",
    "                m[word[r]] = 1\n",
    "                arr.append(word[r])\n",
    "\n",
    "                if len(m.keys()) == 5:\n",
    "                    res = max(res, len(arr))\n",
    "            else:\n",
    "                m = {}\n",
    "                arr = []\n",
    "                l = r\n",
    "                arr.append(word[r])\n",
    "                m[word[r]] = 1\n",
    "            r += 1\n",
    "        \n",
    "                \n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        # 窗口\n",
    "        left = right = 0\n",
    "        window = []\n",
    "        cnt = set()\n",
    "        res = 0\n",
    "        while right<len(word):\n",
    "            if not window or word[right]>=window[-1]:\n",
    "                window.append(word[right])\n",
    "                cnt.add(word[right])\n",
    "                if len(cnt) == 5:\n",
    "                    res = max(len(window),res)\n",
    "            else:\n",
    "                window = []\n",
    "                cnt = set()\n",
    "                left = right\n",
    "                window.append(word[left])\n",
    "                cnt.add(word[left])\n",
    "            right+=1\n",
    "        return res\n",
    "\n",
    "\n",
    "\n",
    "                    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        # 直接计算最长的字符串，如果破坏规则，那么就重新计数\n",
    "        # 如果当前字符是a，那么就从当前字符开始，否则就重新找下一个a，如果没有满足的条件返回0\n",
    "        # \n",
    "        beautiful_len = 0\n",
    "        beautiful_list = []\n",
    "        beauitful_dict = {\n",
    "            \"a\": 1,\n",
    "            \"e\": 2,\n",
    "            \"i\": 3,\n",
    "            \"o\": 4,\n",
    "            \"u\": 5\n",
    "        }\n",
    "        max_len = 0\n",
    "        if len(word) < 5:\n",
    "            return beautiful_len\n",
    "\n",
    "        for index in range(len(word)):\n",
    "            if len(beautiful_list) == 0:\n",
    "                if word[index] == 'a':\n",
    "                    beautiful_list.append(word[index])\n",
    "                    beautiful_len = beautiful_len + 1\n",
    "                else:\n",
    "                    beautiful_len = 0\n",
    "                    beautiful_list = []\n",
    "            elif beautiful_list[-1] == word[index]:\n",
    "                beautiful_list.append(word[index])\n",
    "                beautiful_len = beautiful_len + 1\n",
    "            elif beauitful_dict.get(word[index]) - beauitful_dict.get(beautiful_list[-1]) == 1:\n",
    "                beautiful_len = beautiful_len + 1\n",
    "                beautiful_list.append(word[index])\n",
    "            else:\n",
    "                if len(set(beautiful_list)) == 5 and len(beautiful_list) > max_len:\n",
    "                    max_len = len(beautiful_list)\n",
    "                if word[index] == 'a':\n",
    "                    beautiful_list = ['a']\n",
    "                    beautiful_len = 1\n",
    "                else:\n",
    "                    beautiful_len = 0\n",
    "                    beautiful_list = []\n",
    "        if len(set(beautiful_list)) == 5 and len(beautiful_list) > max_len:\n",
    "            max_len = len(beautiful_list)\n",
    "        return max_len\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        item = [0]*len(word)\n",
    "        res = 0\n",
    "        i = 0\n",
    "        dic = set()\n",
    "        while i <len(word):\n",
    "            dic.clear()\n",
    "            if word[i]=='a':\n",
    "                item = 1\n",
    "                index = 0\n",
    "                if len(word) - i < 5:\n",
    "                    break\n",
    "                dic.add(word[i])\n",
    "                for j in range(i+1,len(word)):\n",
    "                    if word[j]>=word[j-1]:\n",
    "                        item+=1\n",
    "                        if word[j] not in dic:\n",
    "                            dic.add(word[j])\n",
    "                    else:\n",
    "                        index = 1\n",
    "                        break\n",
    "                if index == 1 and item >= 5 and len(dic)==5:\n",
    "                    res = max(res,item)\n",
    "\n",
    "                elif index == 0 and item >= 5 and len(dic)==5:\n",
    "                    res = max(res,item)\n",
    "                i = j\n",
    "            else:\n",
    "                i += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        st = []\n",
    "        d = set()\n",
    "        ans = 0\n",
    "        for c in word:\n",
    "            if st and c < st[-1]:\n",
    "                st = []\n",
    "                d = set()\n",
    "            st.append(c)\n",
    "            d.add(c)\n",
    "            if len(d) == 5: ans = max(ans, len(st))\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        str_ori = [i for i in word]\n",
    "        str_p = ['a','e','i','o','u','']\n",
    "\n",
    "        if set(str_ori) != {'a','e','i','o','u'}:\n",
    "            # print('1')\n",
    "            return 0\n",
    "        point_f = point_s = str_ori.index('a')\n",
    "        b = 0\n",
    "        j = 1\n",
    "        maxmon = 0\n",
    "\n",
    "        while point_f < len(str_ori):\n",
    "            # print('ori',point_s,point_f,str_ori[point_f])\n",
    "            # print('ppp',b,j)\n",
    "        \n",
    "           \n",
    "            if str_ori[point_f] == str_p[j]:\n",
    "                point_f += 1\n",
    "                j += 1\n",
    "                b += 1\n",
    "            elif str_ori[point_f] == str_p[b]:\n",
    "                point_f += 1\n",
    "            else:\n",
    "                if (point_f - point_s) >=5 and str_ori[point_f-1] == 'u':\n",
    "                    maxmon = max(maxmon,(point_f - point_s))\n",
    "                    # print('zheli')\n",
    "                if 'a' in str_ori[point_f:]:\n",
    "                    point_f = point_s = str_ori[point_f:].index('a') + point_f\n",
    "                    b = 0\n",
    "                    j = 1\n",
    "                else:\n",
    "                    return maxmon \n",
    "            if j == 5 and str_ori[point_f - 1] == 'u': \n",
    "                maxmon = max(maxmon,(point_f - point_s))\n",
    "        return maxmon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        if  'a' not in word:\n",
    "            return 0\n",
    "        else: \n",
    "            strl=['a','e','i','o','u']\n",
    "            sb=''\n",
    "            sl=[]\n",
    "            slist=[]\n",
    "            N=0\n",
    "            for i in range(len(word)):\n",
    "                if sb=='' and word[i]=='a':\n",
    "                    sb='a'\n",
    "                    sl.append(sb)\n",
    "                    N=1\n",
    "                elif sb==word[i]:\n",
    "                    sl.append(sb)\n",
    "                elif word[i]==strl[N]:\n",
    "                    sl.append(word[i])\n",
    "                    sb=word[i]\n",
    "                    if N<4:\n",
    "                        N+=1\n",
    "                elif word[i]<sb or word[i]>strl[N]:\n",
    "                    count=0\n",
    "                    for s in strl:\n",
    "                        if s in sl:\n",
    "                            count+=1\n",
    "                    if count==5:\n",
    "                        slist.append(len(sl))\n",
    "                    sl=[]\n",
    "                    N=1\n",
    "                    sb=''\n",
    "                    if word[i]=='a':\n",
    "                        sl.append(word[i])\n",
    "                        sb=word[i]\n",
    "                \n",
    "                            \n",
    "                    \n",
    "            if len(slist)==0:\n",
    "                count=0\n",
    "                for s in strl:\n",
    "                        if s in sl:\n",
    "                            count+=1\n",
    "                if count==5:\n",
    "                    return len(sl)\n",
    "                else:\n",
    "                        return 0\n",
    "            else:\n",
    "                return max(slist)\n",
    "            \n",
    "            \n",
    "        \n",
    "        \n",
    "                \n",
    "            \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        left, right = 0, 0\n",
    "        cnt = set()     #字母种类\n",
    "        window = list()\n",
    "        ans = 0\n",
    "        while right < len(word):\n",
    "            if not window or word[right]>=window[-1]:\n",
    "                window.append(word[right])\n",
    "                cnt.add(word[right])\n",
    "                if len(cnt) == 5:\n",
    "                    ans = max(ans, len(window))\n",
    "            else:\n",
    "                left = right\n",
    "                cnt = set()\n",
    "                window = list()\n",
    "                window.append(word[right])\n",
    "                cnt.add(word[right])\n",
    "            right += 1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        if len(word) < 5:\n",
    "            return 0\n",
    "        tmp_list = []\n",
    "        length_list = []\n",
    "        for i, j in groupby(word):\n",
    "            tmp_list.append(i)\n",
    "            length_list.append(len(list(j)))\n",
    "\n",
    "        res = 0\n",
    "        for i in range(0, len(length_list) - 4):\n",
    "            cur = tmp_list[i: i+5]\n",
    "            if cur == ['a', 'e', 'i', 'o', 'u']:\n",
    "                res = max(res, sum(length_list[i: i+5]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        if len(word) < 5:\n",
    "            return 0\n",
    "        tmp_list = []\n",
    "        length_list = []\n",
    "        for i, j in groupby(word):\n",
    "            tmp_list.append(i)\n",
    "            length_list.append(len(list(j)))\n",
    "\n",
    "        res = 0\n",
    "        for i in range(0, len(length_list) - 4):\n",
    "            cur = tmp_list[i: i+5]\n",
    "            if cur == ['a', 'e', 'i', 'o', 'u']:\n",
    "                res = max(res, sum(length_list[i: i+5]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        str_ori = [i for i in word]\n",
    "        dict_list = {'a':('a','e'),'e':('e','i'),'i':('i','o'),'o':('o','u'),'u':'u'}\n",
    "        if set(str_ori) != {'a','e','i','o','u'}:\n",
    "            return 0\n",
    "        the_first = the_boottom = str_ori.index('a')\n",
    "        maxmon = 0\n",
    "        while the_first < len(str_ori)-1:\n",
    "            print(str_ori[the_boottom],str_ori[the_first])\n",
    "            if str_ori[the_first + 1] in dict_list[str_ori[the_first]]:\n",
    "                the_first += 1\n",
    "            else:\n",
    "                if str_ori[the_first] == 'u':\n",
    "                    maxmon = max(maxmon, (the_first - the_boottom) + 1)\n",
    "                if 'a' in str_ori[the_first+1:]:\n",
    "                    the_first = the_boottom = str_ori[the_first+1:].index('a') + the_first+1\n",
    "                else:\n",
    "                    return maxmon\n",
    "        if str_ori[the_first] == 'u':\n",
    "            return max(maxmon, (the_first - the_boottom)+1)\n",
    "        else:\n",
    "            return maxmon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        if not word:\n",
    "            return 0\n",
    "\n",
    "        record = [-1] * len(word)\n",
    "        max_word = 'n'\n",
    "\n",
    "        if 'a' == word[0]:\n",
    "            record[0] = 1\n",
    "            max_word = word[0]\n",
    "\n",
    "        for i  in range(1, len(word)):\n",
    "            if 'u' == word[i]:\n",
    "                if max_word >= 'o':\n",
    "                    record[i] = 2\n",
    "                    max_word = 'u'\n",
    "                else:\n",
    "                    max_word = 'n'\n",
    "\n",
    "            elif 'a' == word[i]:\n",
    "                if max_word != 'a' and max_word != 'u':\n",
    "                    record[i-1] = -1\n",
    "                record[i] = 1\n",
    "                max_word = 'a'\n",
    "\n",
    "            elif 'e' == word[i]:\n",
    "                if max_word == 'e' or max_word=='a':\n",
    "                    record[i] = 1\n",
    "                    max_word = 'e'\n",
    "                else:\n",
    "                    max_word = 'n'\n",
    "            elif 'i' == word[i]:\n",
    "                if max_word == 'i' or max_word=='e':\n",
    "                    record[i] = 1\n",
    "                    max_word = 'i'\n",
    "                else:\n",
    "                    max_word = 'n'\n",
    "                    \n",
    "            elif 'o' == word[i]:\n",
    "                if max_word == 'i' or max_word=='o':\n",
    "                    record[i] = 1\n",
    "                    max_word = 'o'\n",
    "                else:\n",
    "                    max_word = 'n'\n",
    "            \n",
    "            else:\n",
    "                pass\n",
    "        \n",
    "        start, end = len(record) - 1, len(record) - 1\n",
    "        \n",
    "        result_index = []\n",
    "\n",
    "        while start >= 0:\n",
    "            while end >= 0 and record[end] != 2:\n",
    "                end -= 1\n",
    "                start -= 1\n",
    "            \n",
    "            if start >= 0:\n",
    "                pre = record[start]\n",
    "                start = start - 1\n",
    "                while start >= 0 and record[start] >= 1 and record[start] <= pre:\n",
    "                    pre = record[start]\n",
    "                    start -= 1\n",
    "\n",
    "                \n",
    "                result_index += [[start + 1, end + 1]] \n",
    "                end = start\n",
    "            else:\n",
    "                break\n",
    "\n",
    "        result_index = [i[1] - i[0] for i in result_index]\n",
    "        if result_index:\n",
    "            return (max(result_index))\n",
    "        else:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        stk = []\n",
    "        ans = 0\n",
    "        for ch in word:\n",
    "            if ch == 'a':\n",
    "                if not stk or stk[-1] == 'a':\n",
    "                    stk.append(ch)\n",
    "                else:\n",
    "                    stk = ['a']\n",
    "            elif ch == 'e':\n",
    "                if stk and stk[-1] in ('a', 'e'):\n",
    "                    stk.append(ch)\n",
    "                else:\n",
    "                    stk = []\n",
    "            elif ch == 'i':\n",
    "                if stk and stk[-1] in ('e', 'i'):\n",
    "                    stk.append(ch)\n",
    "                else:\n",
    "                    stk = []\n",
    "            elif ch == 'o':\n",
    "                if stk and stk[-1] in ('i', 'o'):\n",
    "                    stk.append(ch)\n",
    "                else:\n",
    "                    stk = []\n",
    "            else:\n",
    "                if stk and stk[-1] in ('o', 'u'):\n",
    "                    stk.append(ch)\n",
    "                    ans = max(ans, len(stk))\n",
    "                else:\n",
    "                    stk = []\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        result = 0\n",
    "        A = [[c, len(list(s))] for c, s in itertools.groupby(word)]\n",
    "\n",
    "        for i in range(len(A) - 4):\n",
    "            if ''.join(A[j][0] for j in range(i, i + 5)) == 'aeiou':\n",
    "                result = max(result, sum(A[j][1] for j in range(i, i + 5)))\n",
    "        \n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word):\n",
    "        res = 0\n",
    "        A = [[c, len(list(s))] for c,s in itertools.groupby(word)]\n",
    "        print(A)\n",
    "        for i in range(len(A) - 4):\n",
    "            if ''.join(A[j][0] for j in range(i, i+5)) == 'aeiou':\n",
    "                res = max(res, sum(A[j][1] for j in range(i, i+5)))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# import copy\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        res = 0\n",
    "        A = [[c, len(list(s))] for c,s in itertools.groupby(word)]\n",
    "        print(A)\n",
    "        for i in range(len(A) - 4):\n",
    "            if ''.join(A[j][0] for j in range(i, i+5)) == 'aeiou':\n",
    "                res = max(res, sum(A[j][1] for j in range(i, i+5)))\n",
    "        return res\n",
    "        # y=[0]\n",
    "        # while len(word)>4 and word.index('a')<=len(word)-5:\n",
    "        #     left=word.index('a')\n",
    "        #     c=['a','e','i','o','u']\n",
    "        #     s=['a']\n",
    "        #     for i in range(left+1,len(word)):\n",
    "        #         if word[i]>=word[i-1]:\n",
    "        #             if word[i]>word[i-1]:\n",
    "        #                 if word[i]!=c[len(s)]:\n",
    "        #                     word=word[i:]\n",
    "        #                     break\n",
    "        #                 else:\n",
    "        #                     s.append(word[i])\n",
    "        #             if s==c and i==len(word)-1:\n",
    "        #                 y.append(len(word)-left)\n",
    "        #                 word=[]\n",
    "        #         else:\n",
    "        #             if s==c:\n",
    "        #                 y.append(i-left)\n",
    "        #             word=word[i:]\n",
    "        #             break\n",
    "        # y1=max(y)\n",
    "        # return y1\n",
    "            \n",
    "        # n=0\n",
    "        # a=['a','e','i','o','u']\n",
    "        # i=0\n",
    "        # while i<len(word)-4:\n",
    "        #     if word[i]=='a':\n",
    "        #         s=['a']\n",
    "        #         l=n\n",
    "        #         for j in range(i+1,len(word)):\n",
    "        #             if j==len(word)-1 and word[j]=='u' and (word[j-1]=='e' or word[j-1]=='u'):\n",
    "        #                 return max(l,j-i+1)\n",
    "        #             if word[j]==word[j-1]:\n",
    "        #                 continue\n",
    "        #             elif word[j]==a[a.index(word[j-1])+1]:\n",
    "        #                 s.append(word[j])\n",
    "        #             else:\n",
    "        #                 i=j\n",
    "        #                 if len(s)==5:\n",
    "        #                     n=max(l,j-i)\n",
    "        #                 break\n",
    "        # return n\n",
    "            # for i in a:\n",
    "            #     l=n\n",
    "            #     for j in range(i+4,len(word)):\n",
    "            #         if word[j]=='u':\n",
    "            #             c=[num for num in word[i:j+1]]\n",
    "            #             b=copy.deepcopy(c) #不能用=号 否则改变b也会改变c\n",
    "            #             b.sort()\n",
    "            #             d=list(set(b))\n",
    "            #             d.sort()\n",
    "            #             if b==c and d==['a','e','i','o','u']:\n",
    "            #                 n=max(l,j-i+1)\n",
    "            # return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word):\n",
    "        res = 0\n",
    "        A = [[c, len(list(s))] for c,s in itertools.groupby(word)]\n",
    "        print(A)\n",
    "        for i in range(len(A) - 4):\n",
    "            if ''.join(A[j][0] for j in range(i, i+5)) == 'aeiou':\n",
    "                res = max(res, sum(A[j][1] for j in range(i, i+5)))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        ll = []\n",
    "        for i in word:\n",
    "            if i == 'a':\n",
    "                ll.append(1)\n",
    "            if i == 'e':\n",
    "                ll.append(2)\n",
    "            if i == 'i':\n",
    "                ll.append(3)\n",
    "            if i == 'o':\n",
    "                ll.append(4)\n",
    "            if i == 'u':\n",
    "                ll.append(5)\n",
    "\n",
    "        prevStart = -1\n",
    "        end = -1\n",
    "        len_sofar = 0\n",
    "        prevVal = ''\n",
    "        for (k,v) in enumerate(ll):\n",
    "            if prevStart == -1:\n",
    "                if v != 1:\n",
    "                    continue\n",
    "                else:\n",
    "                    prevStart = k\n",
    "                    prevVal = v\n",
    "                    continue\n",
    "            else:\n",
    "                if v >= prevVal:\n",
    "                    end = k\n",
    "                    prevVal = v\n",
    "                    if v == 5:\n",
    "                        val1 = word[prevStart: end+1]\n",
    "                        if 'a' in val1 and 'e' in val1 and 'i' in val1 and 'o' in val1 and 'u' in val1:\n",
    "\n",
    "                            len_sofar = max(len_sofar, (end-prevStart)+1)\n",
    "                else:\n",
    "                    prevStart = -1\n",
    "                    if v == 1:\n",
    "                        prevStart = k\n",
    "                        prevVal = v\n",
    "        if len_sofar < 5:\n",
    "            return 0\n",
    "        return len_sofar\n",
    "\n",
    "\n",
    "a = Solution()\n",
    "print(a.longestBeautifulSubstring('\"ueoiauoiaeeouaiuiaeoeouiaoaeiueuaiooiueaiaeouoaeiu\"'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        def test(s):\n",
    "            if len(s) == 0:\n",
    "                return 'a'\n",
    "            if s[-1] == 'a':\n",
    "                return 'ae'\n",
    "            elif s[-1] == 'e':\n",
    "                return 'ei'\n",
    "            elif s[-1] == 'i':\n",
    "                return 'io'\n",
    "            elif s[-1] == 'o':\n",
    "                return 'ou'\n",
    "            elif s[-1] == 'u':\n",
    "                return 'u'\n",
    "            else:\n",
    "                return 'a'\n",
    "\n",
    "\n",
    "        res = 0\n",
    "        tmp = []\n",
    "        for i in word:\n",
    "            if i in test(tmp):\n",
    "                tmp.append(i)\n",
    "                continue\n",
    "            if len(tmp) > 0 and tmp[-1] == 'u':\n",
    "                res = max(len(tmp), res)\n",
    "                tmp.clear()\n",
    "                if i == 'a':\n",
    "                    tmp.append('a')\n",
    "            else:\n",
    "                tmp.clear()\n",
    "                if i == 'a':\n",
    "                    tmp.append('a')\n",
    "        if len(tmp) > 0 and tmp[-1] == 'u':\n",
    "            res = max(res,len(tmp))\n",
    "        return res\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        res = []\n",
    "        lens = []\n",
    "        for ch, b in groupby(word):\n",
    "            res.append(ch)\n",
    "            lens.append(len(list(b)))\n",
    "        n = len(res)\n",
    "        ans = 0\n",
    "        for i in range(n - 4):\n",
    "            if res[i: i + 5] == list('aeiou'):\n",
    "                ans = max(ans, sum(lens[i: i + 5]))\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        if len(word) < 5:\n",
    "            return 0\n",
    "        tmp_list = []\n",
    "        length_list = []\n",
    "        for i, j in groupby(word):\n",
    "            tmp_list.append(i)\n",
    "            length_list.append(len(list(j)))\n",
    "\n",
    "        res = 0\n",
    "        for i in range(0, len(length_list) - 4):\n",
    "            cur = tmp_list[i: i+5]\n",
    "            if cur == ['a', 'e', 'i', 'o', 'u']:\n",
    "                res = max(res, sum(length_list[i: i+5]))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        res = []\n",
    "        lens = []\n",
    "        for ch, b in groupby(word):\n",
    "            res.append(ch)\n",
    "            lens.append(len(list(b)))\n",
    "        n = len(res)\n",
    "        ans = 0\n",
    "        for i in range(n - 4):\n",
    "            if res[i: i + 5] == list('aeiou'):\n",
    "                ans = max(ans, sum(lens[i: i + 5]))\n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        n = len(word)\n",
    "        if n < 5:\n",
    "            return 0\n",
    "        ans = 0\n",
    "        stack = [word[0]]\n",
    "        for i in range(1, n):\n",
    "            if word[i] >= stack[-1]:\n",
    "                stack.append(word[i])\n",
    "                if i == n - 1:\n",
    "                    if len(set(stack)) == 5:\n",
    "                        ans = max(ans, len(stack))\n",
    "            else:\n",
    "                if len(set(stack)) == 5:\n",
    "                    ans = max(ans, len(stack))\n",
    "                stack = [word[i]]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def longestBeautifulSubstring(self, word: str) -> int:\n",
    "        # print(set(word))\n",
    "        # if set(word) != {'a', 'u', 'o', 'e', 'i'}:\n",
    "        #     return 0\n",
    "\n",
    "\n",
    "\n",
    "        # w = set(word)\n",
    "        # if 'a' not in w or 'e' not in w or 'i' not in w or 'o' not in w or 'u' not in w:\n",
    "        #     return 0\n",
    "        # r = re.findall('a+e+i+o+u+', word)\n",
    "        # # print(r)\n",
    "        # if r:\n",
    "        #     # print(max([len(x) for x in r]))\n",
    "        #     return max([len(x) for x in r])\n",
    "        # else:\n",
    "        #     return 0 \n",
    "        # word = 'aaeuaaeioouuui'\n",
    "        # word = \"aeeeiiiioooauuuaeiou\"\n",
    "        # word = \"eieuuoeeueauueeoiiueaeuioeiiieioaouoeeoiiauiueiiuieueeeoaeaeoooiuauiaaueieoeeeeioauaouueiioeiaaaaooeaaaeoouoaoioauoaeuiioaiuuiaaaiieiuiuoiaeuuiooeiiuoaauoioeaeieaeeeiouieuaaaaaeoaeiaeeeaoaeooueieaaaeieooouueaueeiaiuoioiioieuoeeoeaaeeaauoaiaoaauaauioaueiaiooeoiiaiauaooiiauaiaoaoeaioeiaeeuueioauoueuaaiiouoeoeoiaeeooaauieoeaiueioiaieouiaeauaeiaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooouuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuiuaeouiioieouieeiuaeeuoiooioiiaoueioeuaeeoaauoaaeeaaoiuueoeaeoaaeueoueeiiiuiouieaiuoieeoooueiiiaeeuaaieeaaoiuuoeuoauouiieuaeeiueaeeeaaoieaeuiuoeuoaaiueieiouiiiaoueuuiuooeioaeooaeueouiaeuaaiueuoeeiouioiiueiueaaiuoaieuiaoeuoeaeaiuaaoeeuouiiioeeoueuuauoieiaoaeeaeioouoaiieueoauaoeeoeouaiaoaueioiuaooouiueuieuauieeoeaiouoaoeeeueuoeuieooaaaaaiuoououiuuueoioieueuueaueaeuaieueeaieioieuuuueoooueueoiaoiuuiooiauaaiiiiuoaaueeaiiaeaauaooooeaaeuiaaouooauaaaaiieiaeeeeeiauiiuaueieuieaoeeiauiuaueouaeoaaoiuaoiaiouaiaeoiaooieeioiieieioeeioeuioeiooouiiioaiaeeaaieouoaooaiaiueeuieoiuaeuieeiuaoaueiouoieauieiuoooieuiaooeaeaoiueaeeuieaioiuoaeieuaaeouuoiieioeoiaeuiiiuiuaaaoeaaaoiuauaiiuaaaaiieeueeoieuiueuaueiiaioaoaoueiieueoauuueeeeuuueoiuuaiaaeuauuiaeuuaeiaiiioiooeaeiaeoeaeouuaouueeiieaoaeoaueuaoouoioeiaiuiouaueuaiaoeouoeoooioiuioaeeoeaaoioeiioiuoaiiuioiiioeuiiaaaieaoaaeoaeieuiiiuieiieaaioaueoiiiuueeiouuueoiiaeuuaeaaiauaeeieoaioiiuueuaoieieiuiaioeaeuiiaioaieeeaoaieoooiuiaeuuuieeaeeeeuoeueioeiiaaieaiaiiieiouueaouoooueeoiauooeieeoeuoueioauieoeuooiiaiueeouiiuuiaiuuouoaaiuiiiouuuaiiiuiiuouiioaoauiaoeiioaooeuoaauaiiieiuueuueioeaioaaieuuoioeaeeeuauouuiaauuiauuuaiiiooaiaeaeeuioaueeaoiaioiieooueaieoeieoiaouuuuioaieauioeuoauauaaeaoueeiaaoooauoioueaieeaaiuuiieieaaaeoiueeoeoeoaaoioaueeoeoiooeiioiaoeieieaeiaaaoeeuoiueaueoeoaueeiioauuuoaiaeaooaiaaauuieaioeaaoeioouaiauiaaoieiiieeioeeuaiauuaeeooiuuuaeeieaeieoaaiioaoooeaaeaooouaaoeaoiiuaiauooaiuoeauoeiaeaaeueeiaiooaeaoioeuuauaioaiiioioeuuueiuaeoeaiiuaioiueooioeiueaaeueeiauoiaoeooiuaeuoaououiiiauuaaeeuaiuueaaoioueouaooaaiieeeiueieeouoieiuoueeueauauoueaooaoiuouuiauaeoiouaoeaauaaaoaaueoaeaoeuaiiiaueeiiaaoeuaouiaaiauoeeueueiaooeiooaeuooiieioaaeiueuieuoueiaeiiiaaueaaaoauiauaooaeoeouaaoeeoieuioeuooeaeeoioeeoouieiouuooeaioooouoaoeeouueioaeuoeoiioeouoeuiuuaaouaiuaioaiiuiiuuieaoiiieaaeeioeeiuuouioiuoaiuuuouoeauuoauoeiuuoiiuauoaeuieaiaaoaoiieoeeiaoaueuuoiaauioueeiueeaouioiiauoeuaooiueaaeuuiiiooioeiuioaeuuaouieeiuooaeeaeiiiaiieoaeeoaoioieiuiaeiaiiiouoeoeaeuiuaoeuiiouoaeioiouioiaouiaoooioooieiieaeeaiaaiuoaaaaeoauioiouaaaoeiiueaooeieieueaoiuiaiueaaauiuiuooeaaaaeoiooeueiaaueeaeaoouoioouoeouaoaooauieeuuoeooaauuoaueoaaauiiueoaauiiaaiaoaiuoeeooiiauaueeuoiuooaiueeeauaoeeaiieeoeaaaoueaeiouaouioauoouaeoiauaauouuuaoeuaaeueeeouoaouueaooiueeauuiiioeaeeieoaoeaooauaiiiueoaiauoueeiooueeiooiaouauiaoooioaaaaoeiaieoeaeaeeoaoaeeaeeuuuuioouaioeouuiuouuoeoaeeaaaeeaoooiaeieiuiuauouuieuiauooioiuoeeoieooeeouoiaaoueoaaiaiuauaauouiauiuoiioiiiauiuooiaooeeiuoeaoaeouuauueiueiuoioeiiaiioueeuiiiueoaieuueaoaiioeeuuauuuiuaueooeaaeeioieuieuuieaeoeoaaoeaoaoueiiaueuouiuuaauiiiuuoaaouaeioioooieoaioeouiiuoeaiaiiueoauaaiaeaaeouaeoiouieuaiaeuieuuioueiueueuaooiiouaioeuooeuaaoooieuauuaueiieoeiouaoaeuiaoaooeoeiaoieeaiiuaeaeauoaieeoeoeoauiauiaooouioaoiueuiueaueaaaoaouoeauueaieeuaiueiuauiueaoieoaioouiaaeuiiiueuaiuaiaeiaouooeioaoeaoiaieaeiuouaieeieoaiaaoioaeau\"\n",
    "\n",
    "        # 法一：\n",
    "        # a, e, i, o, u = 0, 0, 0, 0, 0\n",
    "        # max_len = 0\n",
    "        # max_str = ''\n",
    "        # for item in word:\n",
    "        #     if not(e + i + o + u) and item == 'a':\n",
    "        #         a +=1\n",
    "        #     elif a and not (i + o + u) and item == 'e':\n",
    "        #         e += 1\n",
    "        #     elif a and e and not (o + u) and item == 'i':\n",
    "        #         i += 1\n",
    "        #     elif a and e and i and not (u) and item == 'o':\n",
    "        #         o += 1\n",
    "        #     elif a and e and i and o and item == 'u':\n",
    "        #         u += 1\n",
    "        #     else:\n",
    "        #         # print(a, e, i, o, u)\n",
    "        #         if a and e and i and o and u and a+e+i+o+u > max_len:\n",
    "        #             # print(a, e, i, o, u)\n",
    "        #             max_len = a+e+i+o+u\n",
    "        #             max_str = (a, e, i, o, u)\n",
    "        #         a, e, i, o, u = 0, 0, 0, 0, 0\n",
    "\n",
    "        #         if item == 'a':\n",
    "        #             a += 1\n",
    "        # if a and e and i and o and u and a+e+i+o+u > max_len:\n",
    "        #     # print(a, e, i, o, u)\n",
    "        #     max_len = a+e+i+o+u\n",
    "        #     max_str = (a, e, i, o, u)\n",
    "        # # print(max_len)\n",
    "        # # print(max_str)\n",
    "        # return max_len\n",
    "\n",
    "\n",
    "        # 法二：\n",
    "        # singlar_word = ''\n",
    "        # per_word_cnt = []\n",
    "        # for i in range(len(word)):\n",
    "        #     if i == 0 or word[i] != word[i-1]:\n",
    "        #         singlar_word += word[i]\n",
    "        #         per_word_cnt.append(1)\n",
    "        #     else:\n",
    "        #         per_word_cnt[-1] += 1\n",
    "\n",
    "        # print(singlar_word)\n",
    "        # print(per_word_cnt)\n",
    "\n",
    "        # res=0\n",
    "        # for i in range(len(word)-4):\n",
    "        #     if singlar_word[i:i+5] == 'aeiou':\n",
    "        #         res = max(res, sum(per_word_cnt[i:i+5]))\n",
    "        # print(res)\n",
    "        # return res\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # word = \"aeiaaioaaaaeiiiiouuuooaauuaeiu\" # 13  解释：最长子字符串是 \"aaaaeiiiiouuu\" ，长度为 13 。\n",
    "        \n",
    "        # word += ' '\n",
    "\n",
    "        # shrink_word = ''\n",
    "        # char_cnt = []\n",
    "\n",
    "        # cur_cnt = 0\n",
    "        # flag = False\n",
    "        # pre_char = word[0]\n",
    "        # for i in word:\n",
    "        #     if i == pre_char:\n",
    "        #         flag = True\n",
    "        #         cur_cnt += 1\n",
    "        #     elif i != pre_char:\n",
    "        #         shrink_word += pre_char\n",
    "        #         char_cnt.append(cur_cnt)\n",
    "        #         cur_cnt = 1\n",
    "                \n",
    "        #     pre_char = i\n",
    "\n",
    "        # print(shrink_word)\n",
    "        # print(char_cnt)\n",
    "\n",
    "        # max_len = 0\n",
    "        # for i in range(len(shrink_word)):\n",
    "        #     if shrink_word[i:i+5] == 'aeiou' and sum(char_cnt[i:i+5]) > max_len:\n",
    "        #         max_len = sum(char_cnt[i:i+5])\n",
    "\n",
    "        # print(max_len)\n",
    "        # return max_len\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        res = []\n",
    "        lens = []\n",
    "        for ch, b in groupby(word):\n",
    "            res.append(ch)\n",
    "            lens.append(len(list(b)))\n",
    "        n = len(res)\n",
    "        ans = 0\n",
    "        for i in range(n - 4):\n",
    "            if res[i: i + 5] == list('aeiou'):\n",
    "                ans = max(ans, sum(lens[i: i + 5]))\n",
    "        return ans "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
